package ru.paswd.marketprice.storage.receipt.app.controllers;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RestController;
import ru.paswd.marketprice.storage.receipt.api.ReceiptStorageApi;
import ru.paswd.marketprice.storage.receipt.api.dto.EditableReceipt;
import ru.paswd.marketprice.storage.receipt.api.dto.FiscalId;
import ru.paswd.marketprice.storage.receipt.api.dto.Receipt;
import ru.paswd.marketprice.storage.receipt.api.dto.ReceiptItem;
import ru.paswd.marketprice.storage.receipt.api.dto.ReceiptSerialized;
import ru.paswd.marketprice.storage.receipt.app.mappers.FiscalIdMapper;
import ru.paswd.marketprice.storage.receipt.app.mappers.ReceiptStorageMapper;
import ru.paswd.marketprice.storage.receipt.app.services.ReceiptItemService;
import ru.paswd.marketprice.storage.receipt.app.services.ReceiptService;

import java.util.List;

@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ReceiptStorageController implements ReceiptStorageApi {
    private final ReceiptService receiptService;
    private final ReceiptItemService receiptItemService;
    private final ReceiptStorageMapper mapper;
    private final FiscalIdMapper fiscalIdMapper;

    @Override
    public Page<Receipt> getAllReceipts(Pageable pageable) {
        return receiptService.getAllReceipts(pageable)
                .map(mapper::toDto);
    }

    @Override
    public Receipt getReceiptByParams(String fiscalNumber, String fiscalSign, String fiscalDocumentNumber) {
        return mapper.toDto(receiptService.getReceiptByParams(fiscalNumber, fiscalSign, fiscalDocumentNumber));
    }

    @Override
    public void addReceipt(Receipt receipt) {
        receiptService.addReceipt(addReferencesToItems(mapper.fromDto(receipt)));
    }

    @Override
    public void addReceipts(List<Receipt> receipts) {
        receipts.forEach(this::addReceipt);
    }

    @Override
    public void addReceiptSerialized(ReceiptSerialized receipt) {
        receiptService.addReceipt(addReferencesToItems(mapper.fromDto(receipt)));
    }

    @Override
    public void addReceiptsSerialized(List<ReceiptSerialized> receipts) {
        receipts.forEach(this::addReceiptSerialized);
    }

    @Override
    public void updateReceipt(EditableReceipt receipt) {
        receiptService.updateReceipt(mapper.fromDto(receipt));
    }

    @Override
    public void removeReceipt(FiscalId fiscalId) {
        receiptService.removeReceipt(fiscalIdMapper.fromDto(fiscalId));
    }

    @Override
    public void addItem(String fiscalNumber, String fiscalSign, String fiscalDocumentNumber, ReceiptItem receiptItem) {
        receiptItemService.addItem(fiscalNumber, fiscalSign, fiscalDocumentNumber, mapper.fromDto(receiptItem));
    }

    @Override
    public void removeItem(String fiscalNumber, String fiscalSign, String fiscalDocumentNumber, String name) {
        receiptItemService.removeItem(fiscalNumber, fiscalSign, fiscalDocumentNumber, name);
    }

    private ru.paswd.marketprice.storage.receipt.app.entity.Receipt addReferencesToItems(final ru.paswd.marketprice.storage.receipt.app.entity.Receipt receipt) {
        receipt.getItems().forEach(i -> i.setReceipt(receipt));
        return receipt;
    }
}
